import { defineStore } from 'pinia'
import i18n from '@/i18n/index'
import { I18NType } from '@/app.config'
import { ElNotification } from 'element-plus';

const languageHis: I18NType = (sessionStorage.getItem('language') === I18NType.EN_US  ? I18NType.EN_US : I18NType.ZH_CN) || I18NType.ZH_CN
const tokenHis: string = (sessionStorage.getItem('token')) || ''
const userHis: any = JSON.parse((sessionStorage.getItem('user')) || '{}')
const socketHis: any = JSON.parse((sessionStorage.getItem('socket')) || '{}')

export default defineStore('layout', {
    state: () => {
        return {
            language: languageHis as I18NType,
            token: tokenHis,
            user: userHis,
            socket: socketHis,
            friendRequestList: [],
            msgHistory: {} as any
        }
    },
    getters: {
        getLanguage(): I18NType{
            return this.language
        },
        getToken(): string {
            return this.token
        },
        getUser(): any {
            return this.user
        },
        getSocket(): any {
            return this.socket
        },
        getFriendRequestList(): any {
            return this.friendRequestList
        },
        getMsgHistory(): any {
            return this.msgHistory
        }
    },
    actions: {
        setLanguage(language: I18NType) {
            this.language = language
            i18n.global.locale.value = language
            sessionStorage.setItem('language', language)
        }, 
        setToken(token: string) {
            this.token = token
            sessionStorage.setItem('token', this.token)
        },
        setUser(data: any) {
            this.user = data
            sessionStorage.setItem('user', JSON.stringify(this.user))
        },
        setSocket(socket: any) {
            this.socket = socket
            // sessionStorage.setItem('socket', JSON.stringify(socket))
        },
        setFriendRequestList(data: any) {
            this.friendRequestList = data
        },
        setMsgHistory(data: any) {
            this.msgHistory = data
        },
        setMsgHistoryByRoomId(roomId: string, data: any) {
            if(!this.msgHistory[roomId]) {
                this.msgHistory[roomId] = []
            }
            this.msgHistory[roomId].push(data)
        } 
    }
})
